import { _decorator, Component, Node } from 'cc';
import { BasePanel } from '../../../../scripts/libs/ui/BasePanel';
import { gm } from '../../../../scripts/managers/GameManager';
import { StageInfo } from '../../game/basic';
import { sys } from 'cc';
import { lm } from '../../LobbyManager';
import { deepclone } from '../../../../scripts/libs/utils/deepclone/deepclone';
import { clipboardm } from '../../../../scripts/managers/ClipboardManager';
import { EditorStageView } from './EditorStageView';
import { EditorConfirmDialogPanel } from './EditorConfirmDialogPanel';
import { KeyDown } from '../../../../scripts/libs/rxjs/cc3/KeyEvent';
import { KeyCode } from 'cc';
import { screen } from 'cc';
const { ccclass, property } = _decorator;

@ccclass('EditorGamePanel')
export class EditorGamePanel extends BasePanel {

    @property(EditorStageView)
    stage: EditorStageView = null;

    @property(Node)
    inspectorContainer: Node = null;

    _skip_overide_stage_key = null;

    willBind(): void {
        this.stage.inspectorContainer = this.inspectorContainer;
    }

    useObserves() {
        return [
            gm.loadStageCommand.subscribe((_: any) => {
                let stageInfo: StageInfo;
                let stageId = Number(_);
                if (!Number.isNaN(stageId)) {
                    let str = sys.localStorage.getItem(`stage_${stageId.toString().padStart(3, '0')}`);
                    if (str != null && str != '') {
                        stageInfo = JSON.parse(str);
                    }
                } else {
                    stageInfo = _;
                }
                if (stageInfo == null) {
                    lm.frontTipShow('Can not find stage!');
                    return;
                }
                gm.stageInfo.value = stageInfo;
                this.stage.loadStage(deepclone(gm.stageInfo.value));
                gm.stageInfoChanged.execute(gm.stageInfo.value);
            }),
            gm.saveStageCommand.subscribe(_ => {
                if (_ == null) {
                    lm.frontTipShow('No stageId, skip saved!');
                    return;
                }
                this.stage.stageInfo.stageId = _;
                gm.stageInfo.value = this.stage.stageInfo;

                let stageKey = `stage_${_.toString().padStart(3, '0')}`

                let confirmFunc = () => {
                    let str = JSON.stringify(gm.stageInfo.value);
                    sys.localStorage.setItem(stageKey, str);
                    clipboardm.copyToClipboardBrowser(str);
                    gm.stageInfoChanged.execute(gm.stageInfo.value);
                    lm.frontTipShow('Saved!');
                    this._skip_overide_stage_key = stageKey;
                }

                let oldStr = sys.localStorage.getItem(stageKey);
                if (stageKey != 'stage_000' && oldStr != null && oldStr != '' && this._skip_overide_stage_key != stageKey) {
                    EditorConfirmDialogPanel.StartProcess(panel => {
                        let editorConfirmDialogPanel = panel as EditorConfirmDialogPanel;
                        editorConfirmDialogPanel.title.value = 'Need confirm again';
                        editorConfirmDialogPanel.content.value = `Override ${stageKey} in localstorage?`;
                    }).subscribe(_ => {
                        if (_ == 'yes') {
                            confirmFunc();
                        } else {
                            lm.frontTipShow('Canceled!');
                        }
                    });
                } else {
                    confirmFunc();
                }
            }),
            KeyDown().subscribe((_: KeyCode) => {
                if (_ == KeyCode.F11 && gm.editAltPressed.value) {
                    if (screen.fullScreen()) {
                        screen.exitFullScreen().then(_ => {
                            gm.fullScreenChangedCommand.execute();
                        });
                    } else {
                        screen.requestFullScreen().then(_ => {
                            gm.fullScreenChangedCommand.execute();
                        });
                    }
                }
            }),
        ];
    }

    start() {
        this.stage.loadStage(deepclone(gm.stageInfo.value));
        gm.stageInfoChanged.execute(gm.stageInfo.value);
    }

}

